App.controller("activitySetCtr",function($scope, $rootScope, $location, $http, $compile, $stateParams, $timeout, path, publicService) {
	
	$scope.search = {};
	 $scope.pubulishShow = false;   //发布活动
	 
	var checkState = [
		                 {
			                value: "",
			      			text: "请选择",
				         },
			             {
			                value: "2",
			      			text: "可编辑",
					     },
			             {
			                value: "1",
			      			text: "已过期",
					     },
			                 
			          ];
	//编辑状态 
	$("#checkState").kendoDropDownList({
	    dataTextField: "text",
	    dataValueField: "value",
	    dataSource: checkState
	    });	

	var urls1= "";
	
	if(userInfo.user_level == 3){    //编辑状态的显示与隐藏
		 $scope.shopName_hidden = true;
		 $scope.editstates = false;
		 urls1 = path+"/server/selectActivityTemplate";  //集团发布的列表
		 $scope.pubulishShow = true;   //发布活动
	}if(userInfo.user_level == 2){
		 $scope.shopName_hidden = false;
		 $scope.editstates = true;
		 urls1 = path+"/server/selectWeiFaBuPage";  //分公司下可以编辑的活动列表
	}
	//表格对象

	$scope.gridOptions = {
			dataSource : {
				transport : {
					read : {
						url :urls1 ,
						type : 'POST',
						dataType : "json",
						contentType : "application/json"
					},
					parameterMap : function(options, operation) {
						if (operation == "read") {
							var sort = [{dir:"desc",field:"create_time"}];
							if(options.sort!=null && options.sort.length>0 ){
								if(options.sort[0].field=="create_time"){
									options.sort[0].field="create_time";
								}else{
									options.sort[0].field="create_time";
								}
								
								sort=options.sort;
							}
							var parameter = {
								page : options.page, //当前页
								pageSize : options.pageSize,
								param:{
									activityName:$scope.search.activity_name,//活动名称
									create_time:$('#startTime').val(),//创建时间(开始时间)
									end_time:$('#endTime').val(), //结束时间
									audit_state:$('#checkState').val(),//编辑状态 
									sort:sort
								}
							};
							console.log('查询参数',parameter);
							return kendo.stringify(parameter);
						}
					},
				},
				pageSize : 10,
				serverPaging : true,
				serverSorting: true,
				schema : {
					data : function(d) {
						return d.result.data;
					},
					total : function(d) {
						return d.result.total; //总条数
					}
				},
			},
			pageable : {  //分页
				pageSizes: [10, 20, 50, 100],  //每页显示记录数
			  },  
			sortable: {   //排序
				//allowUnsort: false,  //允许无序
			    mode: "multiple"     //排序模式：single，multiple
			  },
			editable: false,  //true：单个编辑；popup：弹出新页面编辑
			dataBound: function () {   //序号
		        var rows = this.items();
		        var page = this.pager.page() - 1;
		        var pagesize = this.pager.pageSize();
		        $(rows).each(function () {
		            var index = $(this).index() + 1 + page * pagesize;
		            var rowLabel = $(this).find(".row-number");
		            $(rowLabel).html(index);
		        });
		    },
			columns : [
					{    
						title : "序号",
						field : "p_id", 
						width : "50px",
						template:function(e){
						var html = "<span class='row-number subTitle'></span>";
						return html;
						}
					},
					{
						field : "name",         
						title : "活动名称",     
						width : "100px"
					},
					{
						field : "create_time",         
						title : "创建日期",     
						width : "100px"
					},
					{
						field : "start_time",         
						title : "活动周期",     
						width : "200px",
						template:function(e){
							return e.start_time + "---" + e.end_time;
						}
					},
					{
						field : "update_time",         
						title : "修改日期",     
						width : "200px"
					},
					{
						field : "shops",         
						title : "分公司参与量",     
						width : "100px",
						template:function(e){
							if(e.shops == null){
								return e.shops = "0";
							}else{
								return e.shops
							}
							
						}
					},
					{
						field : "status",         
						title : "编辑状态",     
						width : "100px",
						hidden: $scope.shopName_hidden,
						values: checkState
					},
					{
						field : "",         
						title : "操作",     
						width : "100px",
						template: function(e){
							var	template_html = "";
							console.log(e.audit_state);
							if(userInfo.user_level==2){  //二级
								if(e.status == 2){   //可编辑
									template_html = '<div class="operation k-state-default " ng-click="editPublish(\''+e.p_id+'\')">编辑发布</div>';
								}else if(e.status == 1){
									template_html = '<div class="operation k-state-default " ng-click="lookDetails(\''+e.p_id+'\')">查看</div>';								
								}
					    	}else if(userInfo.user_level==3){  //三级
								if(e.publish_status == 1){   
									template_html = '<div class="operation k-state-default splitButtonTwoc" ng-click="publishList(\''+e.p_id+'\')">发布列表</div>'
														+'<div class="operation k-state-default " ng-click="editDetails(\''+e.p_id+'\')">编辑</div>'
													+'<div class="operation k-state-default splitButtonTwoc" ng-click="lookDetails(\''+e.p_id+'\')">查看</div>';
								}else if(e.publish_status == 2){
									template_html = '<div class="operation k-state-default " ng-click="editDetails(\''+e.p_id+'\')">编辑</div>';								
								}							
					    	}
					    	return template_html;
					    },
					}
			]
	};	
	
	//查询
	$scope.search = function(){
		$scope.grid.dataSource.page(1);
	}	
	//发布活动
	$scope.addActivity = function(){
		var params = {method:"add",backUrl:"activitySet"};
		$location.path("/addUnionOrderActivity/"+angular.toJson(params));
	}
	//查看
	$scope.lookDetails = function(id){
		if(userInfo.user_level == 3){
			var params = {method:"look2",brandId:id,backUrl:"activitySet"};
		}else if(userInfo.user_level == 2){
			var params = {method:"look",brandId:id,backUrl:"activitySet"};
		}
		
		$location.path("/addUnionOrderActivity/"+angular.toJson(params));
	}
	//编辑发布
	$scope.editPublish = function(id){
		var params = {method:"editpulish",brandId:id,backUrl:"activitySet"};
		$location.path("/addUnionOrderActivity/"+angular.toJson(params));
	}
	//编辑
	$scope.editDetails = function(id){
		var params = {method:"edit",brandId:id,backUrl:"activitySet"};
		$location.path("/addUnionOrderActivity/"+angular.toJson(params));
	}
	//发布活动列表
	$scope.publishList = function(id){
		var paramObject = {
				p_id:id
		};
		$http({
			   url: path+"/server/selectPublishList",
			   method: 'POST',
			   data:angular.toJson(paramObject)
			   
		}).success(function(data){
			if(data.code==0){  //成功
				var html = "";
				$.each(data.result,function(index,objs){
					html +='<div class="ordinary">'+objs.name+'</div>'
						+'<div class="ordinary">'+objs.publish_time+'</div>';
					$('#activityList').html($(html));
				});
				$scope.tipWindow.center().open();   //打开弹框 

			}else if(data.code==1){  //失败
				alert("接口错误！请联系管理员！");
			}
		}).error(function(data){
			console.log(data);   
		})

	}
	publicService.initDate("startTime");	//开始时间
	publicService.initDate("endTime");		//结束时间
});